const { Food, Recipe, Post, DietRecord, SportRecord, WeightRecord, SleepRecord } = require('../models');
const foods = require('../data/foods');
const sequelize = require('../config/database');

async function initFoods() {
  try {
    // 等待数据库连接
    await sequelize.authenticate();
    console.log('数据库连接成功');

    // 按正确顺序删除表
    await DietRecord.destroy({ where: {}, force: true });
    await SportRecord.destroy({ where: {}, force: true });
    await WeightRecord.destroy({ where: {}, force: true });
    await SleepRecord.destroy({ where: {}, force: true });
    await Recipe.destroy({ where: {}, force: true });
    await Post.destroy({ where: {}, force: true });
    await Food.destroy({ where: {}, force: true });
    
    console.log('已清除现有数据');

    // 批量插入初始数据
    await Food.bulkCreate(foods);
    console.log('成功插入初始食物数据');

    process.exit(0);
  } catch (error) {
    console.error('初始化食物数据失败:', error);
    process.exit(1);
  }
}

initFoods(); 